Skip to content

Fix MQTT JSON light restoring None color_mode on startup#168608

Merged
jbouwh merged 2 commits into
home-assistant:devfrom
noerstad:fix/mqtt-json-light-restore-color-mode
Apr 20, 2026
Merged

Fix MQTT JSON light restoring None color_mode on startup#168608
jbouwh merged 2 commits into
home-assistant:devfrom
noerstad:fix/mqtt-json-light-restore-color-mode

Conversation

@noerstad
Copy link
Copy Markdown
Contributor

Proposed change

When an optimistic MQTT JSON light was off at the time Home Assistant saved its state, LightEntity.state_attributes stores color_mode: None in the saved attributes (because color_mode is forced to None whenever is_on is False).

On restart, the restore path in MqttLightJson._subscribe_topics would overwrite the correctly initialized _attr_color_mode with None, because:

self._attr_color_mode = last_attributes.get(ATTR_COLOR_MODE, self.color_mode)

dict.get(key, default) returns the stored None value — the default fallback is only used when the key is missing, not when the stored value is None.

Subsequently turning the light on would then raise "does not report a color mode" from LightEntity.state_attributes, because the light reports is_on=True with color_mode=None.

The fix uses or self.color_mode to fall back to the value set by _setup_from_config when the saved attribute is None. All valid ColorMode values are truthy non-empty strings, so this is safe.

This is a follow-up to #167429, which addressed a related initialization path but did not catch the state-restore path.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

When an optimistic MQTT JSON light was off at the time HA saved its
state, `LightEntity.state_attributes` stores `color_mode: None` in the
saved attributes. On restart, the restore path in `_subscribe_topics`
would overwrite the correctly initialized `_attr_color_mode` with
`None`, because `last_attributes.get(ATTR_COLOR_MODE, self.color_mode)`
returns the stored `None` value (the default fallback is only used
when the key is missing).

Turning the light on would then raise
"does not report a color mode" from `LightEntity.state_attributes`.

Use `or self.color_mode` to fall back to the correctly initialized
value when the saved attribute is `None`. All valid `ColorMode` values
are truthy non-empty strings, so this is safe.
@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @emontnemery, @jbouwh, @bdraco, mind taking a look at this pull request as it has been labeled with an integration (mqtt) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of mqtt can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign mqtt Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a startup restore edge case for optimistic MQTT JSON lights where a previously saved color_mode: None could overwrite the correctly initialized _attr_color_mode, leading to errors on the next turn_on.

Changes:

  • Prevent restoring None into _attr_color_mode by falling back to the configured/default color mode.
  • Add a regression test that simulates restoring an OFF state with color_mode: None and verifies turn_on still works.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
homeassistant/components/mqtt/light/schema_json.py Adjusts optimistic restore logic to avoid restoring None into color_mode.
tests/components/mqtt/test_light_json.py Adds regression coverage for restoring an OFF state containing color_mode: None.

@noerstad
Copy link
Copy Markdown
Contributor Author

Test failure seem to be unrelated flakey test. Hopefully a re-run should do it

Copy link
Copy Markdown
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,
Thanks @noerstad 👍

@jbouwh jbouwh added this to the 2026.4.4 milestone Apr 20, 2026
@jbouwh jbouwh merged commit 70ec6fa into home-assistant:dev Apr 20, 2026
48 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants